﻿@{
    ViewBag.Title = "Index";
    ViewBag.First = "派车管理";
    ViewBag.Second = "派车列表";
    Layout = "~/Views/Shared/_BaseLayout.cshtml";
    var list = ViewData["cartype"] as IList<Sc.Domain.BaseItem>;
}
<script src="~/Content/layer-v2.1/layer/extend/layer.ext.js"></script>
<script type="text/javascript">
    var cars = null;
    $(document).ready(function () {
        loadData();
        $(".select2").uedSelect({
            width: 130
        });
    });
    function loadData() {
        var url = "@Url.Action("GetPageData")";
        var load = loadTip("数据加载中，请稍候！", 16, 0);
        $.get(url, function (data) {
            if (data.length > 0) {
                var table = document.getElementById("listData");
                $.each(data, function (index, n) {
                    var row = table.insertRow(table.rows.length);
                    var cbx = row.insertCell(0);
                    cbx.innerHTML = '<input type="checkbox" value=' + n.Id + ' UserId=' + n.UserId + ' />';
                    var uName = row.insertCell(1);
                    uName.innerHTML = n.UserName;
                    var start = row.insertCell(2);
                    start.innerHTML = n.StartAddress;
                    var end = row.insertCell(3);
                    end.innerHTML = n.EndAddress;
                    var time = row.insertCell(4);
                    time.innerHTML = ChangeDateFormat(n.StartTime);
                    var count = row.insertCell(5);
                    count.innerHTML = n.TotalCount;
                    var rm = row.insertCell(6);
                    rm.innerHTML = n.Remark;
                });
                $('.tablelist tbody tr:odd').addClass('odd');
            }
            layer.closeAll();
        }, "json");
    }
    //派车
    function sendCar() {
        var cbx = $("#listData").find("input[type=checkbox]:checked");
        if (cbx.length <= 0) {
            loadTip("请至少选择一条数据进行派车", 2, 1000);
            return false;
        }
        layer.open({
            title: "车辆选择",
            type: 1,
            area: [($(window).width() - 20) + "px", ($(window).height() - 80) + "px"],
            content: $("#carInfo")
        });
        loadCar(cbx);
    }
    //车辆加载
    function loadCar(obj) {
        var tmp = "", u = "";
        for (var i = 0; i < obj.length; i++) {
            if (i > 0) { tmp += ","; u += ","; }
            tmp += obj[i].value;
            u += $(obj[i]).attr("UserId");
        }
        var url = '@Url.Action("GetNoSendCar", "CarInfo")';
        var load = loadTip("数据加载中，请稍候", 16, 0);
        $.get(url, { "cartype": 0 }, function (data) {
            cars = data;
            var table = document.getElementById("carList"); $("#carList").find("li").remove();
            table.removeAttribute("tmp"); table.removeAttribute("count"); table.setAttribute("tmp", tmp); table.setAttribute("count", obj.length);
            table.removeAttribute("u"); table.setAttribute("u", u);
            showdata();
            layer.close(load)
        });
    }

    function showdata() {
        var cartype = $("#cartypes").val();
        var carno = $("#txt_carno").val();
        var table = document.getElementById("carList");
        $("#carList").find("li").remove();
        var cbx = $("#listData").find("input[type=checkbox]:checked");
        var type = "checkbox";
        if (cbx.length > 1) type = "radio";
        $.each(cars, function (index, n) {
            if (n.CarNo.indexOf(carno) > -1) {
                if (cartype != 0) {
                    if (cartype == n.CarType) {
                        var li = document.createElement("li");
                        html = '<div class="lright"><h2>车牌号：' + n.CarNo + '<input id=' + n.Id + ' type="' + type + '" name="group" style="margin-left:15px;" /></h2><p>座位数：' + n.TakeCount + '<br />驾驶员：<label Driver=' + n.Driver + '> ' + n.DriverName + '</label><a class="enter" onclick="loadDriver(this);">修改驾驶员</a></p></div>';
                        li.innerHTML = html;
                        table.appendChild(li);
                    }
                } else {
                    var li = document.createElement("li");
                    html = '<div class="lright"><h2>车牌号：' + n.CarNo + '<input id=' + n.Id + ' type="' + type + '" name="group" style="margin-left:15px;" /></h2><p>座位数：' + n.TakeCount + '<br />驾驶员：<label Driver=' + n.Driver + '> ' + n.DriverName + '</label><a class="enter" onclick="loadDriver(this);">修改驾驶员</a></p></div>';
                    li.innerHTML = html;
                    table.appendChild(li);
                }
            }
        });

    }
    var temp, cmDriver;
    //加载驾驶员
    function loadDriver(obj) {
        temp = obj;
        cmDriver = layer.open({
            title: "驾驶员选择",
            type: 1,
            area: ["280px", "120px"],
            content: $("#driverInfo")
        });
        var url = '@Url.Action("GetDriver","CarInfo")';
        var load = loadTip("驾驶员加载中，请稍候", 16, 0);
        $.get(url, function (data) {
            if (data.length > 0) {
                var sel = document.getElementById("selDriver"); $("#selDriver").find("option").remove();
                $.each(data, function (index, n) {
                    var option = document.createElement("option");
                    option.text = n.Name;
                    option.value = n.Id;
                    sel.appendChild(option);
                });
                var curId = $(obj).prev().attr("Driver");
                var curText = $(obj).prev().text();
                $("#selDriver").prev().find("em[class=uew-select-text]").text(curText);
                $("#selDriver").val(curId);
            }
            layer.close(load);
        });
    }
    //确定选择的驾驶员
    function sureDriver() {
        var id = $("#selDriver").val();
        var text = $("#selDriver").find("option:selected").text();
        var o = $(temp).prev();
        o.text(text); o.attr("Driver", id);
        layer.close(cmDriver);
    }
    //保存派车
    function saveData() {
        var table = $("#carList");
        var data = new Array();
        var count = parseInt(table.attr("count"));
        if (count == 1) {
            var li = table.find("li").find("input[type=checkbox]:checked");
            for (var i = 0; i < li.length; i++) {
                var ent = new Object();
                ent.CarUseNo = table.attr("tmp");
                ent.ApplicationId = table.attr("u");
                ent.Driver = $(li[i]).parent().next().find("label").attr("Driver");
                ent.CarNo = li[i].id;
                data.push(ent);
            }
        } else {
            var tmp = table.attr("tmp").split(',');
            var mu = table.attr("u").split(',');
            var mp = table.find("input[type=radio]:checked");
            for (var i = 0; i < tmp.length; i++) {
                var ent = new Object();
                ent.CarUseNo = tmp[i];
                ent.ApplicationId = mu[i];
                ent.Driver = mp.parent().next().find("label").attr("Driver");
                ent.CarNo = mp[0].id;
                data.push(ent);
            }
        }
        if (data.length <= 0) {
            loadTip("请至少选择一辆车", 2, 1000);
            return false;
        }
        var url = '@Url.Action("SaveData")';
        var load = loadTip("正在派车，请稍候", 16, 0);
        $.post(url, { data: JSON.stringify(data) }, function (data) {
            if (data > 0) {
                loadTip("派车成功", 1, 1000);
                var cbx = $("#listData").find("input[type=checkbox]:checked");
                for (var i = 0; i < cbx.length; i++) {
                    $(cbx[i]).parent().parent().remove();
                }
            } else {
                loadTip("派车失败", 2, 1000);
            }
            layer.closeAll();
        });
    }
    //拒绝派车
    function refuseSendCar() {
        var cbx = $("#listData").find("input[type=checkbox]:checked");
        if (cbx.length <= 0) {
            loadTip("请至少选择一条数据进行操作", 2, 1000);
            return false;
        }
        if (cbx.length >1) {
            loadTip("只能选择一条数据进行操作", 2, 1000);
            return false;
        }
        layer.prompt({
            title: '请输入拒绝派车原因',
            formType: 2 
        }, function (pass) {
            var url = '@Url.Action("RefuseSendCar")';
            var load = loadTip("正在拒绝派车，请稍候", 16, 0);
            $.post(url, { id: cbx[0].value, opition: pass }, function (data) {
                if (data > 0) {
                    loadTip("拒绝派车成功", 1, 2000);
                    $(cbx[0]).parent().parent().remove();
                } else {
                    loadTip("拒绝派车失败", 2, 2000);
                }
                layer.closeAll();
            });
        });
    }
</script>
<div class="rightinfo">
    <div class="tools">
        <ul class="toolbar">
            <li class="click" onclick="sendCar();"><span><img src="~/Content/images/t01.png" /></span>派车</li>
            <li class="click" onclick="refuseSendCar();"><span><img src="~/Content/images/t03.png" /></span>拒绝派车</li>
        </ul>
    </div>
    <table class="tablelist">
        <thead>
            <tr>
                <th width="30px"></th>
                <th width="10%">申请人</th>
                <th width="10%">起点</th>
                <th width="10%">终点</th>
                <th width="15%">用车时间</th>
                <th width="10%">人数</th>
                <th>备注</th>
            </tr>
        </thead>
        <tbody id="listData"></tbody>
    </table>
    <!--车辆选择-->
    <div id="carInfo" style="display:none;margin-left:20px;">
        <div class="tools" style="margin-top:10px;">
            <ul class="toolbar">
                <li class="click" onclick="saveData();"><span><img src="~/Content/images/check.png" /></span>保存</li>
                <li class="click" onclick="layer.closeAll();"><span><img src="~/Content/images/t03.png" /></span>关闭</li>
                <li style="background:none;border:0;">
                    车辆类型：
                    <select style="opacity:initial" id="cartypes" onChange="showdata()">
                        <option selected value="0">请选择车辆类型</option>
                        @foreach (var item in list)
                        {
                            <option value="@item.Id">@item.Name</option>
                        }
                    </select>
                </li>
                <li style="background:none;border:0">
                    车牌号：
                    <input type="text" id="txt_carno" onkeyup="showdata()" class="scinput" style="width:120px" />
                </li>
            </ul>
        </div>
        <ul class="classlist" id="carList"></ul>
    </div>
    <!--驾驶员选择-->
    <div id="driverInfo" style="display:none;margin-left:20px;">
        <div class="tools" style="margin-top:10px;">
            <ul class="toolbar">
                <li>
                    <select name="select" class="select2" id="selDriver">
                        <option value="-1">----请选择----</option>
                    </select>
                </li>
                <li class="click" onclick="sureDriver();"><span><img src="~/Content/images/check.png" /></span>确定</li>
            </ul>
        </div>

    </div>
</div>


